package com.ch.clinking.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.activity.AddOrderActivity;
import com.ch.clinking.activity.EditMerStockActivity;
import com.ch.clinking.bean.Merchandise;

import java.util.List;

public class MerListAdapter extends ArrayAdapter<Merchandise> {

    private List<Merchandise> merchandiseList;
    private int resourceId;
    private Context context;
    private FragmentManager fragmentManager;


    boolean color = true;

    private List<String> size;

    public MerListAdapter(@NonNull Context context, int resource, @NonNull List<Merchandise> merchandises, FragmentManager fragmentManager) {
        super(context, resource, merchandises);
        this.context = context;
        this.merchandiseList = merchandises;
        this.resourceId = resource;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.merImg = convertView.findViewById(R.id.list_spu_img);
            viewHolder.merTitle = convertView.findViewById(R.id.list_spu_title);
            viewHolder.merSKCId = convertView.findViewById(R.id.list_skc_id);
            viewHolder.merSPUId = convertView.findViewById(R.id.list_spu_id);
            viewHolder.productNum = convertView.findViewById(R.id.list_SKC_product_num);
            viewHolder.tableLayout = convertView.findViewById(R.id.stock_table);
            viewHolder.listMerItemContainer = convertView.findViewById(R.id.list_mer_item_container);
            viewHolder.oderMake = convertView.findViewById(R.id.oder_make);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Merchandise merchandise = merchandiseList.get(position);

        // 设置数据
        viewHolder.merTitle.setText(merchandise.getTitle());
        viewHolder.merSKCId.setText(merchandise.getSkcId());
        viewHolder.merSPUId.setText(merchandise.getSpuId());
        viewHolder.productNum.setText(merchandise.getProductNumber());

        // 清空表格内容
        viewHolder.tableLayout.removeAllViews();

        // 动态生成表格内容
        getSize(merchandise, viewHolder.tableLayout);

//         加载图片
//        Glide.with(context)
//                .load(OkHttpUtil.getInstance().getIMG_URL() + merchandise.getFirstImage())
//                .into(viewHolder.merImg);


        // 设置默认占位图片
        Glide.with(context)
                .load(R.drawable.appicon)
                .into(viewHolder.merImg);

        // 加载图片
        Glide.with(context)
                .load(OkHttpUtil.getInstance().getIMG_URL() + merchandise.getFirstImage())
                .placeholder(R.drawable.appicon)
                .diskCacheStrategy(DiskCacheStrategy.ALL)  // 缓存策略：缓存原始图片和转换后的图片
                .thumbnail(0.5f)  // 缩略图大小
                .error(R.drawable.appicon)
                .into(viewHolder.merImg);

        // 设置点击事件
        viewHolder.listMerItemContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, EditMerStockActivity.class);
                intent.putExtra("merchandise", merchandise);
                context.startActivity(intent);
            }
        });

        viewHolder.oderMake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AddOrderActivity.class);
                intent.putExtra("merchandise", merchandise);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    // ViewHolder 类
    static class ViewHolder {
        ImageView merImg;
        TextView merTitle;
        TextView merSKCId;
        TextView merSPUId;
        TextView productNum;
        TableLayout tableLayout;
        ViewGroup listMerItemContainer;
        Button oderMake;
    }


    @SuppressLint("SetTextI18n")
    private void getSize(Merchandise merchandise, TableLayout tableLayout) {
        color = true;
        TableRow sizeRow = new TableRow(context);
        TableRow stockRow = new TableRow(context);
        sizeRow.setPadding(0, 0, 0, 0);
        stockRow.setPadding(0, 0, 0, 0);
        TableRow.LayoutParams editTextParams = new TableRow.LayoutParams(120, TableRow.LayoutParams.MATCH_PARENT);
        editTextParams.gravity = Gravity.CENTER;

        if (merchandise.isActivate_XXS()) {
            TextView size = new TextView(context);
            size.setText("XXS");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_XXS()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_XS()) {
            TextView size = new TextView(context);
            size.setText("XS");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_XS()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_S()) {
            TextView size = new TextView(context);
            size.setText("S");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_S()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_M()) {
            TextView size = new TextView(context);
            size.setText("M");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_M()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_L()) {
            TextView size = new TextView(context);
            size.setText("L");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_L()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_XL()) {
            TextView size = new TextView(context);
            size.setText("XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_XXL()) {
            TextView size = new TextView(context);
            size.setText("XXL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_XXL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_Plus_0XL()) {
            TextView size = new TextView(context);
            size.setText("0XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_Plus_0XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_Plus_1XL()) {
            TextView size = new TextView(context);
            size.setText("1XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_Plus_1XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_Plus_2XL()) {
            TextView size = new TextView(context);
            size.setText("2XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_Plus_2XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_Plus_3XL()) {
            TextView size = new TextView(context);
            size.setText("3XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_Plus_3XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_Plus_4XL()) {
            TextView size = new TextView(context);
            size.setText("4XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_Plus_4XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_Plus_5XL()) {
            TextView size = new TextView(context);
            size.setText("5XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_Plus_5XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }
        if (merchandise.isActivate_Plus_6XL()) {
            TextView size = new TextView(context);
            size.setText("6XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(merchandise.getInHouseStock_Plus_6XL()));
            setTableLayout(size, stock, sizeRow, stockRow, editTextParams);
        }

        tableLayout.addView(sizeRow);
        tableLayout.addView(stockRow);
    }

    private void setTableLayout(TextView size, TextView stock, TableRow sizeRow, TableRow stockRow, TableRow.LayoutParams editTextParams) {
        size.setLayoutParams(editTextParams);
        stock.setLayoutParams(editTextParams);
        size.setBackgroundColor(getCellColor());
        stock.setBackgroundColor(getCellColor());
        if (color) {
            color = false;
        } else {
            color = true;
        }
        size.setWidth(120);
        stock.setWidth(120);
        size.setPadding(0, 0, 0, 0);
        stock.setPadding(0, 0, 0, 0);
        size.setGravity(Gravity.CENTER);
        stock.setGravity(Gravity.CENTER);
        sizeRow.addView(size);
        stockRow.addView(stock);
    }

    private int getCellColor() {
        if (color) {
            return Color.parseColor("#E7E7E7"); // 偶数索引
        } else {
            return Color.parseColor("#FFFFFF"); // 奇数索引
        }
    }

}
